FormPicker   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 12
eloc 39
dl 0
loc 78
rs 10
c 0
b 0
f 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
A createPicker 0 19 4
A getPickerElements 0 13 3
A constructor 0 5 1
A init 0 18 4
1
/** global: dtsel */
2
3
/**
4
 * This class dynamicaly adds a date or time - picker to all configured fields.
5
 */
6
class FormPicker
7
{
8
	/**
9
	 * pass the id of the form element to the constructor.
10
	 * @param string id
11
	 */
12
    constructor(config) 
13
	{
14
        this.config = config;
15
        this.aPickers = [];
16
    }
17
18
	/**
19
	 * Init all date fields.
20
	 */
21
    init() 
22
	{
23
        var i;
24
        var aPickerElements = this.getPickerElements();
25
        var length = aPickerElements.length;
26
        for (i = 0; i < length; i++) {
27
			// since getPickerElements() only return existing items with 'data-picker' attribute
28
			// there's no need to check this values against null
29
			let item = aPickerElements[i];
30
            let picker = item.getAttribute('data-picker');
31
            // we can't use the split function because the param may contain the ':' itself (time-separator...)
32
			// let [type, param] = validate.split(':');
33
            let pos = picker.indexOf(':');
34
            let type = (pos >= 0) ? picker.substring(0, pos) : picker;
35
            let param = (pos >= 0) ? picker.substring(pos + 1) : '';
36
            this.createPicker(item, type, param);
37
        }
38
    }
39
40
	/**
41
	 * Get all elements inside the form with the attribute 'data-picker' set.
42
	 * @returns array
43
	 */
44
    getPickerElements() 
45
	{
46
        let pickerElements = [];
47
        let form = document.getElementById(this.config.FormDataValidation.formID);
48
        let formElements = form.getElementsByTagName('*');
49
        let length = formElements.length;
50
        for (let i = 0; i < length; i++) {
51
            if (formElements[i].getAttribute('data-picker') !== null) {
52
                pickerElements.push(formElements[i]);
53
            }
54
        }
55
        return pickerElements;
56
    }
57
58
	/**
59
	 * Create the picker and attach to the item.
60
     * @param element oItem item to create the picker for
61
     * @param string strType 'date' or 'time'
62
     * @param string strParam time/date format for the picker to create
63
	 */
64
    createPicker(oItem, strType, strParam) 
65
	{
66
        let cfg = {};
67
        if (typeof this.config.DTSel !== 'undefined') {
68
            Object.assign(cfg, this.config.DTSel); 
69
        }
70
        if (strType === 'date') {
71
            cfg.showDate = true;
72
            cfg.showTime = false;
73
            cfg.dateFormat = strParam;
74
        } else if (strType === 'time'){
75
            cfg.showDate = false;
76
            cfg.showTime = true;
77
            cfg.timeFormat = strParam;
78
            cfg.showSeconds = (strParam.length > 5 );
79
        }
80
        
81
        this.aPickers.push(new dtsel.DTS(oItem, cfg));
82
    }
83
}
84